package com.cuit.dao;

import com.cuit.doamin.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao {

    /**
     * 修改密码
     *
     * @return
     */
    @Update("update user set u_pwd = #{u_pwd} where u_name = #{u_name}")
    public int updatePwd(@Param("u_name") String u_name, @Param("u_pwd") String u_pwd);

    /**
     * 上传简历
     *
     * @return
     */
    @Update("update user set u_resume = #{u_resume} where u_name = #{u_name}")
    public int upload(@Param("u_name") String u_name, @Param("u_resume") String u_resume);

    /**
     * 删除用户的简历
     *
     * @return
     */
    @Update("update user set u_resume = null where u_name = #{u_name}")
    public int delete(String u_name);

    /**
     * 查询用户的简历
     *
     * @return
     */
    @Select("select u_resume from user where u_name = #{u_name}")
    public String findResume(String u_name);

    /**
     * 查询用户投过的招聘_多表查询
     *
     * @return
     */
    @Select("select * from user where u_name = #{u_name}")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "u_id", column = "u_id"),
            @Result(property = "u_name", column = "u_name"),
            @Result(property = "u_pwd", column = "u_pwd"),
            @Result(property = "u_resume", column = "u_resume"),
            @Result(property = "recruitments", column = "u_id",
                    many = @Many(
                            select = "com.cuit.dao.RecruitmentDao.findByUid",
                            fetchType = FetchType.LAZY
                    )
            )
    })
    public List<User> findRecruit(String u_name);

    /**
     * 根据姓名查询ID
     *
     * @param name
     * @return
     */
    @Select("select u_id from user where u_name = #{name}")
    public Integer findIdByName(String name);

    /**
     * 根据招聘ID，投简历到指定的招聘
     *
     * @param uid
     * @param rid
     * @return
     */
    @Insert("insert into r_u (rid,uid) values (#{rid},#{uid})")
    public boolean postResume(@Param("uid") Integer uid, @Param("rid") Integer rid);

    /**
     * 根据招聘ID查询用户信息，此处用到user和r_u两个表
     *
     * @return
     */
    @Select("select * from user LEFT OUTER JOIN r_u on u_id = uid where rid = #{id}")
    public List<User> findByRid(Integer id);
}
